#r "nuget: Newtonsoft.Json, 13.0.3"

using System.Data;
using Newtonsoft.Json;

public class NatsumeSchedule
{
    public string gameweek;
    public List<DateModel> days = new List<DateModel>();

    public class DateModel
    {
        public string date;
        public EventsHolder events = new EventsHolder();
    }
    public class EventsHolder
    {
        public List<ScheduleItem> day = new List<ScheduleItem>();
        public List<ScheduleItem> evening = new List<ScheduleItem>();
        public List<ScheduleItem> night = new List<ScheduleItem>();
    }
    public class ScheduleItem
    {
        public string title;
        public string footnote;
    }
}

var dataModel = JsonConvert.DeserializeObject<List<NatsumeSchedule>>(File.ReadAllText("../out/GameWeeks.json"));

StringBuilder readmeBuilder = new StringBuilder();

readmeBuilder.Append(File.ReadAllText("../src/Readme_Part0.md"));
readmeBuilder.AppendLine();
readmeBuilder.Append(File.ReadAllText("../src/Readme_Extra.md"));
readmeBuilder.AppendLine();

void printEvent(string title, List<NatsumeSchedule.ScheduleItem> i)
{
    if (i.Count > 0)
    {
        readmeBuilder.AppendLine("**"+title+"**");
        readmeBuilder.AppendLine();
        for (int j = 0; j < i.Count; j++)
        {
            var item = i[j];
            var shouldHaveNewLineAfter = item.footnote != null;
            readmeBuilder.AppendLine("- "+item.title + (shouldHaveNewLineAfter ? "   " : ""));
            if (item.footnote != null)
            {
                readmeBuilder.AppendLine("  *" + item.footnote + "*");
            }
        }
        readmeBuilder.AppendLine();
    }
}
readmeBuilder.AppendLine();
readmeBuilder.AppendLine("---");
readmeBuilder.AppendLine();
readmeBuilder.AppendLine("# 攻略正文");
readmeBuilder.AppendLine();
foreach (var gameWeek in dataModel)
{
    readmeBuilder.AppendLine("## "+gameWeek.gameweek);
    foreach (var gameDay in gameWeek.days)
    {
        readmeBuilder.AppendLine();
        readmeBuilder.AppendLine("### " + gameDay.date + "");
        readmeBuilder.AppendLine();
        printEvent("白天",gameDay.events.day);
        printEvent("傍晚",gameDay.events.evening);
        printEvent("夜晚",gameDay.events.night);
    }
    readmeBuilder.AppendLine();
}


if(!Directory.Exists("../out")){
    Directory.CreateDirectory("../out");
}

File.WriteAllText("../out/README.md",readmeBuilder.ToString());